#!/bin/bash
# SPDX-License-Identifier: GPL-2.0

#
# This file is part of Lustre, http://www.lustre.org/
#
# LUSTRE-VERSION-GEN
#
# Return lustre version string
#

DEFAULT_VERSION=2.16.58
LVF=LUSTRE-VERSION-FILE

LF='
'

if test -d ${GIT_DIR:-.git} -o -f .git &&
	VN=$(git describe --match "[0-9]*" --abbrev=7 HEAD 2>/dev/null) &&
	case "$VN" in
	*$LF*) (exit 1) ;;
	[0-9]*)
		git update-index -q --refresh
		test -z "$(git diff-index --name-only HEAD --)" ||
		VN="$VN-dirty" ;;
	esac
then
	VN=$(echo "$VN" | sed -e 's/-/_/g');
elif test -r $LVF
then
	VN=$(sed -e 's/^LUSTRE_VERSION = //' <$LVF)
else
	VN="$DEFAULT_VERSION"
fi

if test -r $LVF
then
	VC=$(sed -e 's/^LUSTRE_VERSION = //' <$LVF)
else
	VC=unset
fi
test "$VN" = "$VC" || {
	echo "LUSTRE_VERSION = $VN" >$LVF
}

echo $VN
